package cn.edu.ujn.lab3.web;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.lab3.dao.User;
import cn.edu.ujn.lab3.service.UserService;
import cn.edu.ujn.lab3.util.Page;


@Controller
public class UserController {
	// 依赖注入
	@Autowired
	private UserService userService;
	
	@GetMapping("/login")
	public String toLogin() {
		return "login";
	}
	
	/**
	 * 用户登录
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	public String login(String usercode,String password, Model model, HttpSession session) {
		// 通过账号和密码查询用户
		User user = userService.findUser(usercode, password);
		if(user != null){		
			// 将用户对象添加到Session
			session.setAttribute("USER_SESSION", user);
			//return "customer";
			return "redirect:customer/list.action";
		}
		model.addAttribute("msg", "账号或密码错误，请重新输入！");
         // 返回到登录页面
		return "login";
	}
	
	/**
	 * 模拟其他类中跳转到客户管理页面的方法
	 */
	@RequestMapping(value = "/toCustomer.action")
	public String toCustomer() {
	    return "customer";
	}
	
	/**
	 * 退出登录
	 */
	@RequestMapping(value = "/logout.action")
	public String logout(HttpSession session) {
	    // 清除Session
	    session.invalidate();
	    // 重定向到登录页面的跳转方法
	    return "redirect:login.action";
	}
	
	/**
	 * 向用户登陆页面跳转
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.GET)
	public String toLogin1() {
	    return "login";
	}
	
	/**
	 * 用户列表
	 */
	@RequestMapping(value = "/user/list.action")
	public String list(@RequestParam(defaultValue = "1") Integer page,
					   @RequestParam(defaultValue = "10") Integer rows,
					   String userCode, String userName, Integer userState, Model model) {
		Page<User> userList = userService.findUserList(page, rows, userCode, userName, userState);

		model.addAttribute("page", userList);
		model.addAttribute("userCode", userCode);
		model.addAttribute("userName", userName);
		model.addAttribute("userState", userState);

		return "user";
	}
	/**
	 * 用户创建
	 */
	@RequestMapping(value = "/user/create.action")
	@ResponseBody
	public String userCreate(User user) {
		int rows = userService.createUser(user);
		if(rows > 0){
			return "OK";
		}else{
			return "FAIL";
		}
	}
	/**
	 * 根据id查询用户
	 */
	@RequestMapping(value = "/user/getUserById.action")
	@ResponseBody
	public User getUserById(Integer id) {
		return userService.getUserById(id);
	}
	/**
	 * 更新用户
	 */
	@RequestMapping("/user/update.action")
	@ResponseBody
	public String userUpdate(User user) {
		int rows = userService.updateUser(user);
		if(rows > 0){
			return "OK";
		}else{
			return "FAIL";
		}
	}
	/**
	 * 删除用户
	 */
	@RequestMapping("/user/delete.action")
	@ResponseBody
	public String userDelete(Integer id) {
		int rows = userService.deleteUser(id);
		if(rows > 0){
			return "OK";
		}else{
			return "FAIL";
		}
	}
	
}

